#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"Tree.h"
Tree MakeTree(Tree T) {
	if (T != NULL) {
		MakeTree(T->Left);
		MakeTree(T->right);
		free(T);
	}
	return NULL;
}

Tree Insert(Tree T, ElementType x) {
	if (T == NULL) {
		T = (Tree)malloc(sizeof(Tree));
		if (T == NULL) {
			perror("malloc");
			return NULL;
		}
		T->element = x;
		T->Left = T->right = NULL;
	}
	else
		if (x < T->element) {
			T->Left = Insert(T->Left, x);
		}
		else
			if (x > T->element) {
				T->right = Insert(T->right, x);
			}
	return T;
}

void ShowTree(Tree T) {
	if(T != NULL) {
		ShowTree(T->Left);
		printf("%d ", T->element);
		ShowTree(T->right);
	}
}

Qeue CreatQeue() {
	Qeue Q = malloc(Size * sizeof(Qeue));
	if (Q == NULL) {
		perror("malloc");
	}

	Q->arr = malloc(Size * sizeof(Tree));
	if (Q->arr == NULL) {
		perror("malloc");
	}
	Q->front = Q->rear = 0;
	Q->Capicity = Size;
	Q->size = 0;
	return Q;
}

void Enqeue(Qeue Q, Tree T) {
	if (Q->rear == Q->Capicity) {
		return;
	}

	Q->arr[Q->rear] = T;
	Q->rear++;
	Q->size++;
}

Tree Dnqeue(Qeue Q) {
	if (Q == NULL) {
		printf("Empty!");
	}
	Tree T = Q->arr[Q->front];
	Q->front++;
	Q->size--;
	return T;
}

void LevelTree(Tree T,Qeue Q) {
	if (T == NULL) {
		printf("Empty!");
	}
	Enqeue(Q, T);
	while (Q->size>0) {
		Tree X = Dnqeue(Q);
		printf("%d ", X->element);

		if (X->Left) {
			Enqeue(Q, X->Left);
		}
		if (X->right) {
			Enqeue(Q, X->right);
		}
	}
}